package mall.dao.impl;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import mall.bean.OrderItem;
import mall.bean.OrderItemPK;
import mall.dao.OrderItemDao;

public class OrderItemDaoImpl implements OrderItemDao {

	@Override
	public void save(OrderItem obj)  {
		String sql = "INSERT INTO order_item (oid, pid, quantity) VALUES (?, ?, ?)";
		JdbcTemplate.update(sql, obj.getOid(), obj.getPid(), obj.getQuantity());
	}

	@Override
	public void delete(OrderItemPK id)  {
		String sql = "DELETE FROM order_item WHERE oid = ? AND pid = ?";
		JdbcTemplate.update(sql, id.getOid(), id.getPid());
	}

	@Override
	public void update(OrderItem obj)  {
		String sql = "UPDATE order_item SET quantity = ? WHERE oid = ? AND pid = ?";
		JdbcTemplate.update(sql, obj.getQuantity(), obj.getOid(), obj.getPid());
	}

	@Override
	public OrderItem findById(OrderItemPK id)  {
		String sql = "SELECT oid, pid, quantity FROM order_item WHERE oid = ? AND pid = ?";
		return JdbcTemplate.query(new BeanHandler<OrderItem>(OrderItem.class), sql, id.getOid(), id.getPid());
	}

	@Override
	public long count()  {
		String sql = "SELECT COUNT(1) FROM order_item";
		return JdbcTemplate.query(new ScalarHandler<Long>(), sql);
	}

}
